/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Common.Constants;
import Common.GeneralHandling;
import POJO.userLog;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.*;
import org.apache.thrift.TException;

/**
 *
 * @author ngochuy
 */
public class userLogDAO {
    public static ByteBuffer colParent = ByteBuffer.wrap("userLog".getBytes());
    public static ByteBuffer coluserID = ByteBuffer.wrap("userID".getBytes());
    public static ByteBuffer coluserName = ByteBuffer.wrap("userName".getBytes());
    public static ByteBuffer coltimeLogin = ByteBuffer.wrap("timeLogin".getBytes());
    
    public static String colParentS = "userLog";
    public static String userIDS = "userID";
    public static String userNameS = "userName";
    public static String timeLoginS = "timeLogin";
    
    private static Cassandra.Client client;
    private static userLogDAO instance;
    
    public static userLogDAO getInstance(Cassandra.Client _client)
    {
        if (instance != null)
            return instance;
        instance = new userLogDAO();
        client = _client;
        return instance;
    }
    
    public boolean setuserLog(String userID, String userName) throws UnsupportedEncodingException
    {
        boolean result = false;
        try {
            ColumnParent parent = new ColumnParent(colParentS);
            ByteBuffer id = GeneralHandling.toByteBuffer(GeneralHandling.getTimeUUID().toString());

            //Add các column vào CF.
            client.insert(id, parent,GeneralHandling.createCol(coluserID, userID), Constants.CL);
            client.insert(id, parent,GeneralHandling.createCol(coluserName, userName), Constants.CL);
            client.insert(id, parent,GeneralHandling.createCol(coltimeLogin, ""), Constants.CL);
            result = true;
        } catch (InvalidRequestException ex) {
            Logger.getLogger(userLogDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnavailableException ex) {
            Logger.getLogger(userLogDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(userLogDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(userLogDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
    
    public List<userLog> getuserLog() throws UnsupportedEncodingException
    {
        List<userLog> result = new ArrayList<userLog>();
        try {
            SlicePredicate predicate = new SlicePredicate();
            SliceRange slice = new SliceRange();
            slice.start = Common.GeneralHandling.toByteBuffer("");
            slice.finish = Common.GeneralHandling.toByteBuffer("");
            predicate.slice_range = slice;
            
            KeyRange range = new KeyRange();
            range.start_key = Common.GeneralHandling.toByteBuffer("");
            range.end_key = Common.GeneralHandling.toByteBuffer("");
            List<KeySlice> listkey = client.get_range_slices(new ColumnParent(colParentS), predicate, range, ConsistencyLevel.ONE);
            for (KeySlice keySlice : listkey) {
                result.add(createuserLog(keySlice));
            }
        } catch (InvalidRequestException ex) {
            Logger.getLogger(userLogDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnavailableException ex) {
            Logger.getLogger(userLogDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TimedOutException ex) {
            Logger.getLogger(userLogDAO.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TException ex) {
            Logger.getLogger(userLogDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
    
    private userLog createuserLog(KeySlice temp)
    {
        userLog result = new userLog();
        try {
            result.userID = Common.GeneralHandling.toString(temp.columns.get(0).column.value);
            result.userName = Common.GeneralHandling.toString(temp.columns.get(1).column.value);
            result.timeLogin = Common.GeneralHandling.toString(temp.columns.get(2).column.value);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(userLogDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return result;
    }
}
